﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using MySql.Data.MySqlClient;
using VatTuDAO;
using System.Data;

namespace VatTuDAO1
{
    public class LoaiSanPhamDAO : BaseDataProvider
    {
         MySqlCommand _cmd;
        public List<LoaiSanPhamDTO> SelectLoaiSanPham(int maChungLoai)
        {
            List<LoaiSanPhamDTO> ds = new List<LoaiSanPhamDTO>();
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiSanPham where MaChungLoai=@MaChungLoai";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaChungLoai",maChungLoai));
          
              DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
         
            LoaiSanPhamDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new LoaiSanPhamDTO();
                    dto.MaLoaiSanPham= int.Parse(row["MaLoaiSanPham"].ToString());
                    dto.TenLoaiSanPham = row["TenLoaiSanPham"].ToString();
                    dto.MaChungLoai = int.Parse(row["MaChungLoai"].ToString());
                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }
        //--------------------------
        public LoaiSanPhamDTO SelectLoaiSanPhamTheoMaLoai(int maLoai)
        {
            
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiSanPham where MaLoaiSanPham=@MaLoaiSanPham";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiSanPham", maLoai));

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            LoaiSanPhamDTO dto;
            DataRow row;
            row = dt.Rows[0];
            if (row==null)
            {
                return null;
            }
                dto = new LoaiSanPhamDTO();
                dto.MaLoaiSanPham = int.Parse(row["MaLoaiSanPham"].ToString());
                dto.TenLoaiSanPham = row["TenLoaiSanPham"].ToString();
                dto.MaChungLoai = int.Parse(row["MaChungLoai"].ToString());
           
            return dto;
        }
        //-------------------------------------------------------
        public List<LoaiSanPhamDTO> SelectLoaiSanPham()
        {
            List<LoaiSanPhamDTO> ds = new List<LoaiSanPhamDTO>();
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiSanPham";
            _cmd.CommandType = CommandType.Text;
          

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            LoaiSanPhamDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new LoaiSanPhamDTO();
                    dto.MaLoaiSanPham = int.Parse(row["MaLoaiSanPham"].ToString());
                    dto.TenLoaiSanPham = row["TenLoaiSanPham"].ToString();
                    dto.MaChungLoai = int.Parse(row["MaChungLoai"].ToString());
                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }

        //Them
        public Boolean InsertLoaiSanPham(LoaiSanPhamDTO dto)
        {

            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into loaisanpham";
            _cmd.CommandText += " values(@TenLoaiSanPham,@MaChungLoai)";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@TenLoaiSanPhamm", dto.TenLoaiSanPham));
            _cmd.Parameters.Add(new MySqlParameter("@MaChungLoai", dto.MaChungLoai.ToString()));
           
            if (ExecuteNonQuery(_cmd) == false)
            {
                return false;
            }

            return true;
        }
        //CapNhat
        //--------------------------------
        public Boolean UpdateLoaiSanPham(LoaiSanPhamDTO dto)
        {

            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update LoaiSanPham set TenLoaiSanPham=@TenLoaiSanPham,MaChungLoai=@MaChungLoai,";
            _cmd.CommandText += " where MaLoaiSanPham=@MaLoaiSanPham";
            _cmd.CommandType = CommandType.Text;
            _cmd.CommandType = CommandType.Text;
           _cmd.Parameters.Add(new MySqlParameter("@TenLoaiSanPham", dto.TenLoaiSanPham));
            _cmd.Parameters.Add(new MySqlParameter("@TenNhaSanXuat", dto.MaChungLoai));
           

            if (ExecuteNonQuery(_cmd) == false)
            {
                return false;
            }

            return true;
        }
        //---------------------
        public Boolean DeleteLoaiSanPham(int MaLoaiSanPham)
        {

            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from LoaiSanPham";
            _cmd.CommandText += " where MaLoaiSanPham=@MaLoaiSanPham";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiSanPham", MaLoaiSanPham));

            if (ExecuteNonQuery(_cmd) == false)
            {
                return false;
            }

            return true;
        }
    }
}
